#使用方法
custom_project_path=$(cd `dirname $0`; pwd)
#工程绝对路径
project_path=$1
#工程名
project_name=$2
#scheme名
scheme_name=$3
development_mode=Release
IPADir=$4

#打包模式 Debug/Release
function getdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
getdir $dir_or_file $2
else
if [[ $dir_or_file =~ $2 ]]; then
local configFile="$dir_or_file"
echo "$configFile"
break
fi
fi
done
}

#打包模式 Debug/Release
echo -e "\033[36m请输入版本（1.Release，2.Debug):\033[0m"
read number
while([[ $number != 1 ]] && [[ $number != 2 ]])
do
echo -e "\033[31mError! Should enter 1 or 2\033[0m"
echo -e "\033[36m请输入版本（1.Release，2.Debug):\033[0m"
read number
done

#修改文件内容和plist所在位置
if [ $number == 2 ];then
	GCC_PREPROCESSOR_DEFINITIONS_Adhoc=(DDXQ_BUILD_FOR_DEVELOP=1,"\$(inherited)")
    exportOptionsPlistPath=$(getdir $custom_project_path "exportTest.plist")
else
	GCC_PREPROCESSOR_DEFINITIONS_Adhoc=("\$(inherited)")
    exportOptionsPlistPath=$(getdir $custom_project_path "exportAdhoc.plist")
fi

#build文件夹路径
build_path=${project_path}/build

#导出.ipa文件所在路径
exportIpaPath=${project_path}/IPADir/${development_mode}

##选择发布位置，暂时隐藏
# echo -e "\033[36m请选发布位置（1.app-store，2.ad-hoc):\033[0m"
# read number
# while([[ $number != 1 ]] && [[ $number != 2 ]])
# do
# echo -e "\033[31mError! Should enter 1 or 2\033[0m"
# echo -e "\033[36m请选发布位置（1.app-store，2.ad-hoc):\033[0m"
# read number
# done

#number=2
#
#if [ $number == 1 ];then
#exportOptionsPlistPath=$(getdir $custom_project_path "exportAppstore.plist")
#else
#exportOptionsPlistPath=$(getdir $custom_project_path "exportAddhoc.plist")
#fi

function showText(){
echo -e "\033[34m///--------"
echo -e "///$1"
echo -e "///--------\033[0m"
echo ''
}

showText "正在清理工程"

xcodebuild \
clean -configuration ${development_mode} -quiet  || exit

showText "清理完成"


showText '正在编译工程:'${development_mode}

xcodebuild \
archive -workspace ${project_path}/${project_name}.xcworkspace \
-scheme ${scheme_name} \
-configuration ${development_mode} \
-archivePath ${build_path}/${project_name}.xcarchive \
GCC_PREPROCESSOR_DEFINITIONS=${GCC_PREPROCESSOR_DEFINITIONS_Adhoc}  -quiet  || exit

showText "编译完成"

showText "开始ipa打包"

xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \
-exportPath ${exportIpaPath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-quiet || exit

echo -e "\033[4;31m///----------"
if [ -e $exportIpaPath/$scheme_name.ipa ]; then
echo -e " ipa包已导出"
open $exportIpaPath
else
echo -e " ipa包导出失败"
fi
echo -e "///----------\033[0m"
echo ""


rm -R ${build_path}

showText "开始发布ipa打包"


#if [ $number == 1 ];then
#
##验证并上传到App Store
## 将-u 后面是AppleID的账号，-p后面的是密码
#altoolPath="/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
#username = "dm@shiwaixiangcun.com"
#password = "Swxc,./2017.."
#"$altoolPath" --validate-app -f ${exportIpaPath}/${scheme_name}.ipa -u ${username} -p ${password} -t ios --output-format xml
#"$altoolPath" --upload-app -f ${exportIpaPath}/${scheme_name}.ipa -u  ${username} -p ${password} -t ios --output-format xml
#else

#上传到蒲公英

curl -F "file=@$exportIpaPath/$scheme_name.ipa"\
     -F "uKey=8fac16d27114aaf0978530c4b176d0ec"\
     -F "_api_key=962195fc659af8da06616729390fa5dc"\
     https://qiniu-storage.pgyer.com/apiv1/app/upload
#fi

exit 0
